﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace File_Operate_Program
{
    public partial class FrmCheckEncodingType : Form
    {
        static Singleton singleton = Singleton._instance;

        public FrmCheckEncodingType()
        {
            InitializeComponent();
            this.StartPosition = FormStartPosition.CenterScreen;
        }
        private void FrmCheckEncodingType_Load(object sender, EventArgs e)
        {
            #region 方法赋值
            this.BtnSelectFile.Click += new System.EventHandler(this.BtnSelectFile_Click);
            #endregion
        }

        private void BtnSelectFile_Click(object sender, EventArgs e)
        {
            OpenFileDialog openfile = new OpenFileDialog();
            //初始显示文件目录
            openfile.InitialDirectory = singleton.FilePath;
            openfile.Title = "请选择文件";
            //过滤文件类型
            openfile.Filter = "文本文件|*.csv|文本文件|*.txt|可执行文件|*.exe|所有文件类型|*.*";
            if (DialogResult.OK == openfile.ShowDialog())
            {
                RTxtFileInfo.Clear();

                //将选择的文件的全路径赋值给文本框
                string sFilePath = openfile.FileName;
                Encoding TxtEncoding = CEncodingType.GetType(sFilePath);  //get encode from document . 

                RTxtFileInfo.AppendText($"文件路径：{ sFilePath }\r\n");
                if (TxtEncoding.HeaderName.Contains("utf-8"))
                {
                    var TxtEncodingGetPreamble = TxtEncoding.GetPreamble();
                    if (TxtEncodingGetPreamble.Length != 0 && (TxtEncodingGetPreamble[0] == 0xEF && TxtEncodingGetPreamble[1] == 0xBB && TxtEncodingGetPreamble[2] == 0xBF))
                    {
                        RTxtFileInfo.AppendText($"文件类型：{ TxtEncoding.EncodingName }(带BOM)\r\n");
                    }
                    else
                    {
                        RTxtFileInfo.AppendText($"文件类型：{ TxtEncoding.EncodingName }\r\n");
                    }
                }
                else
                {
                    RTxtFileInfo.AppendText($"文件类型：{ TxtEncoding.EncodingName }\r\n");
                }

                System.IO.FileInfo fileInfo = new System.IO.FileInfo(sFilePath);
                System.Diagnostics.FileVersionInfo info = System.Diagnostics.FileVersionInfo.GetVersionInfo(sFilePath);
                RTxtFileInfo.AppendText($"文件名称：{ info.FileName.Substring(info.FileName.LastIndexOf("\\") + 1) }\r\n");
                RTxtFileInfo.AppendText($"产品名称：{ info.ProductName }\r\n");
                RTxtFileInfo.AppendText($"公司名称：{ info.CompanyName }\r\n");
                RTxtFileInfo.AppendText($"文件版本：{ info.FileVersion }\r\n");
                RTxtFileInfo.AppendText($"产品版本：{ info.ProductVersion }\r\n");
                // 通常版本号显示为「主版本号.次版本号.生成号.专用部件号」
                RTxtFileInfo.AppendText($"系统显示文件版本：{ info.ProductMajorPart }.{info.ProductMinorPart}.{info.ProductBuildPart}.{info.ProductPrivatePart}\r\n");
                RTxtFileInfo.AppendText($"文件说明：{ info.FileDescription }\r\n");
                RTxtFileInfo.AppendText($"文件语言：{ info.Language }\r\n");
                RTxtFileInfo.AppendText($"原始文件名称：{ info.OriginalFilename }\r\n");
                RTxtFileInfo.AppendText($"文件版权：{ info.LegalCopyright }\r\n");

                RTxtFileInfo.AppendText($"文件大小：{ System.Math.Ceiling(fileInfo.Length / 1024.0) }KB\r\n");
            }
        }
    }
}
